#-*- coding:utf-8 -*-
from __future__ import division
import func
#import api
#import numpy as np
# import matplotlib.pyplot as plt
from datetime import date
from dateutil.relativedelta import relativedelta

#销量走势分析
def sell_count(p):
    #获得具体商品
    name = p['productInfo'][0]['name']
    print '========'+name+'========'
    p['review'].sort(key=lambda x: x['publishTime'])
    review_list = [func.get_review_date(r) for r in p['review']]
    # func.print_info(review_list)
    date_list = []
    count_list = []
    if review_list:
        s_date = date(review_list[0].year, review_list[0].month, 1)
        date_list.append(s_date)
        count_list.append(0)
    for d in review_list:
        if date_list[-1].year == d.year and date_list[-1].month == d.month:
            count_list[-1] += 1
        else:
            while True:
                s_date = s_date + relativedelta(months=1)
                #date_list.append(s_date)
                #count_list.append(0)
                if s_date.year == d.year and s_date.month == d.month:
                    count_list.append(0)
                    count_list[-1] += 1
                    date_list.append(s_date)
                    break
    sum_review_list = [sum(count_list[0:(n+1)]) for n in range(len(count_list))]
    # print date_list
    return {'date_list': [[d.year, d.month] for d in date_list],
            'count_list': sum_review_list,
            'trend_list': count_list}
